package cn.biz.imp;

import java.util.ArrayList;
import java.util.List;

import cn.biz.OrdersBiz;
import cn.dao.OrdergoodsDao;
import cn.dao.OrdersDao;
import cn.entity.Ordergoods;
import cn.entity.Orders;
import cn.entity.User;

public class OrdersBizImp implements OrdersBiz {
	private OrdersDao ordersDao;
	private OrdergoodsDao ordergoodsDao;

	public OrdersDao getOrdersDao() {
		return ordersDao;
	}

	public void setOrdersDao(OrdersDao ordersDao) {
		this.ordersDao = ordersDao;
	}

	public OrdergoodsDao getOrdergoodsDao() {
		return ordergoodsDao;
	}

	public void setOrdergoodsDao(OrdergoodsDao ordergoodsDao) {
		this.ordergoodsDao = ordergoodsDao;
	}

	/*
	 * 插入一條orders信息 成功返回一个success字符串
	 * 
	 * @see cn.biz.OrdersBiz#insert(cn.entity.Orders)
	 */
	@Override
	public String insert(Orders orders) {
		String result = "error";
		try {
			ordersDao.insert(orders);
			result = "success";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/*
	 * 删除一条orders信息 成功返回一个success的字符串
	 * 
	 * @see cn.biz.OrdersBiz#delete(cn.entity.Orders)
	 */
	@Override
	public String delete(Orders orders) {
		String result = "error";
		try {
			ordersDao.delete(orders);
			result = "success";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/*
	 * 更新orders订单信息 成功返回一个success的字符串
	 * 
	 * @see cn.biz.OrdersBiz#update(cn.entity.Orders)
	 */
	@Override
	public String update(Orders orders) {
		String result = "error";
		try {
			ordersDao.update(orders);
			result = "success";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public List getAll() {
		return ordersDao.getAll();
	}

	/*
	 * 按照orders的ID查询一条orders记录
	 * @see cn.biz.OrdersBiz#getOrdersById(java.lang.Integer)
	 */
	@Override
	public Orders getOrdersById(Integer id) {
		return ordersDao.getOrdersById(id);
	}

	@Override
	public List<Orders> getOrdersListByBuyer(User user) {
		return ordersDao.getOrdersListByBuyer(user.getId());
	}
	
	/*
	 * 酱油修改 - 20170517
	 * (non-Javadoc)
	 * @see cn.biz.OrdersBiz#getOrdersListBySeller(cn.entity.User)
	 */
	@Override
	public List<Orders> getOrdersListBySeller(User user) {
		
		List<Orders> userOrders = new ArrayList();
		
		List<Orders> list = ordersDao.getAll();
		System.out.println("所有订单: "+list.size());
		for (Orders order : list) {
			List<Ordergoods> ogList = ordersDao.getOrdergoodsByOrderId(order.getId());
			System.out.println("订单号: "+order.getNum());
			if(ogList.size() > 0){
				System.out.println("商品卖家id: "+ogList.get(0).getGoods().getUser().getId()+" 当前用户id: "+user.getId());
				if((ogList.get(0).getGoods().getUser().getId()).equals(user.getId())){//判断该商品快照的商品所属的id卖家是否与当前登陆用户id相同
					System.out.println("添加进去了");
					userOrders.add(order);
				}
			}
		}
		return userOrders;
	}
	
	@Override
	public List<Orders> getOrdersByOrderNum(String num){
		return ordersDao.getOrdersByOrderNum(num);
	}

	@Override
	public List<Ordergoods> getOrdergoodsByOrderId(int id) {
		// TODO Auto-generated method stub
		return ordersDao.getOrdergoodsByOrderId(id);
	}

	@Override
	public List<Integer> getLastWeekCompleteSize(String[] lastWeek) {
		List<Integer> list = new ArrayList<Integer>();
		for (int i = 0; i < lastWeek.length-1; i++) {
			list.add(ordersDao.queryCompleteByDate(lastWeek[i], lastWeek[i+1]).size());
		}
		return list;
	}

}
